package com.xls.future.presenter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.xls.future.entity.Photo;
import com.xls.future.model.HomeModel;
import com.xls.future.model.HomeModelImpl;
import com.xls.future.view.HomeView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import rx.Subscriber;

/**
 * Created by Administrator on 2017/8/28.
 */

public class HomePresenterImpl implements HomePresenter {

    private Context context;
    private HomeModel homeModel;
    private HomeView homeView;

    public HomePresenterImpl(Context context, HomeView homeView) {
        this.context = context;
        this.homeView = homeView;
        homeModel = new HomeModelImpl();
    }


    @Override
    public void loadPicsForBanner() {
        homeModel.loadPicsForBanner(context,new Subscriber<List<Photo>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onNext(List<Photo> photos) {
                Log.d("home","查询成功：共"+photos.size()+"条数据。");
                List<Photo> result = new ArrayList<Photo>();
                if(photos.size()>10){
                    Collections.shuffle(photos);
                    result.addAll(photos.subList(0,10));
                }else {
                    result.addAll(photos);
                }
                homeView.bindDataToBanner(result);
            }
        });
    }


    @Override
    public void loadPicsForStaggerList(final String groupName) {
        homeModel.loadPhotosByPage(context,groupName,new Subscriber<List<Photo>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onNext(List<Photo> photos) {
                Log.d("home","查询瀑布流成功：共"+photos.size()+"条数据。");
                if(TextUtils.isEmpty(groupName)){
                    Collections.shuffle(photos);
                }
                homeView.bindDataToStaggered(photos,false);

            }
        });
    }

    @Override
    public void loadPicsMore(final String groupName,int skipCount) {
        homeModel.loadPhotosMore(context,groupName,skipCount, new Subscriber<List<Photo>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onNext(List<Photo> photos) {
                Log.d("home","加载更多：共"+photos.size()+"条数据。");
                if(TextUtils.isEmpty(groupName)){
                    Collections.shuffle(photos);
                }
                homeView.bindDataToStaggered(photos,true);
            }
        });
    }

}
